package com.aliware.tianchi;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentHashMap;

public class LoadBalanceHelper {
    private static int initSmall = 200;//200
    private static int initMedium = 450;//450
    private static int initLarge = 650;//650

    public static int WSmall = initSmall;
    public static int WMedium = initMedium;
    public static int WLarge = initLarge;

    private static Timer timer = new Timer();

    //当前运行的线程数
    public  static ConcurrentHashMap<Object, Date> CurrentSmall =
            new ConcurrentHashMap<Object, Date>();
    public  static ConcurrentHashMap<Object,Date> CurrentMedium =
            new ConcurrentHashMap<Object, Date>();
    public  static ConcurrentHashMap<Object,Date> CurrentLarge =
            new ConcurrentHashMap<Object, Date>();


    public LoadBalanceHelper()
    {
        timer.schedule(new TimerTask() {
            public void run() {
                Date d = new Date();
                for(Object s: CurrentSmall.keySet())
                {
                    if (d.getTime() - CurrentSmall.get(s).getTime() > 1000) //1秒以上则删除
                    {
                        CurrentSmall.remove(s);
                    }
                }

                for(Object s: CurrentMedium.keySet())
                {
                    if (d.getTime() - CurrentMedium.get(s).getTime() > 1000) //1秒以上则删除
                    {
                        CurrentMedium.remove(s);
                    }
                }

                for(Object s: CurrentLarge.keySet())
                {
                    if (d.getTime() - CurrentLarge.get(s).getTime() > 1000) //1秒以上则删除
                    {
                        CurrentLarge.remove(s);
                    }
                }
            }
        }, 2000 , 1000);
    }
    private int maxW(int x, int y)
    {
        return x>y?x:y;
    }
    private int maxW(int x, int y,int z)
    {
        int mv = maxW(x,y);
        return mv > z ? mv : z;
    }

    public String getInvoker() {
        //return O1();
        return  O2();
    }

    public static int DiffSmall()
    {
        return initSmall - CurrentSmall.size();
    }

    public static int DiffMedium()
    {
        return initMedium - CurrentMedium.size();
    }

    public static int DiffLarge()
    {
        return initLarge - CurrentLarge.size();
    }

    private String O2()
    {
        String result = "";
        int sm = DiffSmall();
        int me = DiffMedium();
        int la = DiffLarge();
        int maxV = maxW(sm,me,la);
        if (maxV == sm && sm > 0)
        {
            result = "small";
        }else if (maxV == me && me > 0)
        {
            result = "medium";
        }else if (maxV == la && la > 0)
        {
            result = "large";
        }
        return result;
    }

    private String O1()
    {
        String result = "large";
        int maxV = maxW(WSmall,WMedium,WLarge);
        if (maxV == WSmall)
        {
            result = "small";
            WSmall --;
        }else if (maxV == WMedium)
        {
            result = "medium";
            WMedium --;
        }else if (maxV == WLarge)
        {
            result = "large";
            WLarge --;
        }

        if (WSmall < 0 && WMedium < 0 && WLarge < 0)
        {
            WSmall = initSmall;
            WMedium = initMedium;
            WLarge = initLarge;

        }
        return result;
    }
}
